<?php
namespace app\api\controller;
use app\home\model\Seo;


class SiteInfo
{
    public function index()
    {
        $seo = new Seo;
        $data = [
            'host'=>$seo->field('value')->where('key', 'host')->find()['value'],
            'title'=>$seo->field('value')->where('key', 'title')->find()['value'],
            'descript'=>$seo->field('value')->where('key', 'desc')->find()['value'],
            'kw'=>$seo->field(['id', 'value'])->where('key', 'kw')->select(),
        ];
        return json($data);
    }
    public function addKw()
    {
        $data = request()->param('value');
        $seo = new Seo;
        $seo->save([
            'key'=>'kw',
            'value'=>$data,
        ]);

        return json([
            'kw'=>[
                'value'=>$seo['value']
            ]
        ]);
    }
    public function delKw()
    {
        $id = request()->param('id');
        Seo::destroy($id);
    }
    public function baseInfo()
    {
        $data = request()->param();
        $seo = new Seo;
        $host = $seo->where('key', 'host')->find();
        $title = $seo->where('key', 'title')->find();
        $desc = $seo->where('key', 'desc')->find();



        if(empty($host)){
            unset($seo);
            $seo = new Seo;
            $seo->save([
                'key'=> 'host',
                'value'=>$data['host']
            ]);
        }else{
            $host->save([
                'value'=>$data['host'],
            ]);
        }

        if(empty($title)){
            unset($seo);
            $seo = new Seo;
            $seo->save([
                'key'=> 'title',
                'value'=>$data['title']
            ]);
        }else{
            $title->save([
                'value'=>$data['title'],
            ]);
        }

        if(empty($desc)){
            unset($seo);
            $seo = new Seo;
            $seo->save([
                'key'=> 'desc',
                'value'=>$data['descript']
            ]);
        }else{
            $desc->save([
                'value'=>$data['descript'],
            ]);
        }
        
    }
}
